package com.wy.w_ui.banner.core;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.wy.w_ui.R;
import com.wy.w_ui.banner.WBanner;
import com.wy.w_ui.banner.adapter.IWBindAdapter;
import com.wy.w_ui.banner.adapter.WBannerAdapter;
import com.wy.w_ui.banner.indicator.IWIndicator;
import com.wy.w_ui.banner.indicator.WCircleIndicator;

import java.util.List;

/**
 * WBanner 辅助类
 * 帮助 WBanner 完成各种功能的控制
 * 将 WBanner 的一些逻辑内聚在这,保证暴露给使用者的 WBanner 干净整洁
 */
public class WBannerHelper implements IWBanner, ViewPager.OnPageChangeListener {

    private Context context;
    private WBanner banner;

    private WBannerAdapter bannerAdapter;
    private IWIndicator indicator;
    private boolean autoPlay;
    private boolean loop;
    private List<? extends WBannerBean> bannerMos;
    private ViewPager.OnPageChangeListener onPageChangeListener;
    private int intervalTime = 5000;
    private WBanner.OnBannerClickListener onBannerClickListener;
    private WViewPager viewPager;
    private int scrollerDuration = -1;

    public WBannerHelper(Context context, WBanner wBanner) {
        this.context = context;
        this.banner = wBanner;
    }

    @Override
    public void setBannerData(int layoutResId, @NonNull List<? extends WBannerBean> models) {
        this.bannerMos = models;
        init(layoutResId);
    }

    @Override
    public void setBannerData(@NonNull List<? extends WBannerBean> models) {
        this.bannerMos = models;
        init(R.layout.banner_def_item);
    }

    @Override
    public void setIndicator(IWIndicator indicator) {
        this.indicator = indicator;
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        this.autoPlay = autoPlay;
        //TODO 两个 setAutoPlay 是否可以变成一个
        if (bannerAdapter != null) {
            bannerAdapter.setAutoPlay(autoPlay);
        }

        if (viewPager != null) {
            viewPager.setAutoPlay(autoPlay);
        }
    }

    @Override
    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void setIntervalTime(int intervalTime) {
        if (intervalTime > 0) {
            this.intervalTime = intervalTime;
        }
    }

    @Override
    public void setBindAdapter(IWBindAdapter bindAdapter) {
        bannerAdapter.setBindAdapter(bindAdapter);
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    @Override
    public void setScroller(int duration) {
        this.scrollerDuration = duration;
        if (viewPager != null && duration > 0) {
            viewPager.setScroller(duration);
        }

    }

    @Override
    public void setOnBannerClickLister(OnBannerClickListener onBannerClickListener) {
        this.onBannerClickListener = onBannerClickListener;
    }

    /**
     * 初始化
     *
     * @param layoutResId
     */
    private void init(int layoutResId) {
        if (bannerAdapter == null) {
            bannerAdapter = new WBannerAdapter(context);
        }

        if (indicator == null) {
            indicator = new WCircleIndicator(context);
        }

        indicator.onInflate(bannerMos.size());

        bannerAdapter.setLayoutResId(layoutResId);
        bannerAdapter.setModels(bannerMos);
        bannerAdapter.setAutoPlay(autoPlay);
        bannerAdapter.setLoop(loop);
        bannerAdapter.setBannerClickListener(onBannerClickListener);

        viewPager = new WViewPager(context);
        viewPager.setIntervalTime(intervalTime);
        viewPager.addOnPageChangeListener(this);
        //TODO 两个 setAutoPlay 是否可以变成一个
        viewPager.setAutoPlay(autoPlay);
        viewPager.setAdapter(bannerAdapter);
        //设置滑动速度
        if (scrollerDuration > 0){
            viewPager.setScroller(scrollerDuration);
        }

        FrameLayout.LayoutParams lPBanner = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        banner.removeAllViews();
        banner.addView(viewPager, lPBanner);
        //添加指示器
        banner.addView(indicator.get(), lPBanner);


    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (null != onPageChangeListener && bannerAdapter.getRealCount() != 0) {
            onPageChangeListener.onPageScrolled(bannerAdapter.getRealPosition(position), positionOffset, positionOffsetPixels);
        }
    }

    @Override
    public void onPageSelected(int position) {

        if (bannerAdapter.getRealCount() == 0) {
            return;
        }

        if (onPageChangeListener != null) {
            onPageChangeListener.onPageSelected(bannerAdapter.getRealPosition(position));
        }

        if (indicator != null) {
            indicator.onPointChange(bannerAdapter.getRealPosition(position), bannerAdapter.getRealCount());
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageScrollStateChanged(state);
        }
    }
}
